<template>
  <div>
    <TopNav :title="$route.meta.title"></TopNav>
    <div class="main">
      <van-address-list
        v-model="chosenAddressId"
        :list="list"
        default-tag-text="默认"
        @add="onAdd"
        @edit="onEdit"
        @select="onSelect"
      />
    </div>
  </div>
</template>

<script>
import TopNav from "@/components/topNav.vue";
import { getAddressList } from "@/api/index";
export default {
  created() {
      this.getAddress();
  },
  data() {
    return {
      chosenAddressId: "1",
      list: [],
    };
  },
  methods: {
    onAdd() {
      sessionStorage.removeItem("addressList")
      this.$router.push({
        path: "/addressAdd",
        query: {
          title: "添加",
        },
      });
    },
    onEdit(item) {
      sessionStorage.removeItem("addressList")
      console.log(item);
      this.$router.push({
        path: "/addressAdd",
        query: {
          title: "编辑",
          id: item.id,
        },
      });
    },
    getAddress() {
      getAddressList().then((data) => {
        this.list = data.data.map((item) => {
          item = {
            id: item.addressId,
            name: item.userName,
            tel: item.userPhone,
            address:
              item.cityName +
              item.provinceName +
              item.regionName +
              item.detailAddress,
            isDefault: item.defaultFlag,
          };
          return item;
        });
      });
    },
    onSelect(item) {
      console.log(item);
    },
  },
  components: { TopNav },
};
</script>
<style scoped>
.main {
  margin-top: 0.8rem;
}
</style>